const {getAllRoles,createRole,deleteRoleById,updateRole} = require('../services/roleService')

module.exports = {
    getAllRoles: async(ctx,next) => {
        try {
            let roles = await getAllRoles()
            if(roles) {
                ctx.body = {
                    success: true,
                    data: {roles: roles},
                    info: {msg: "获取角色列表成功"},
                    code: 200
                }
            } else {
                ctx.body = {
                    success: false,
                    data: {roles: roles},
                    info: {msg: "获取角色列表失败"},
                    code: 200
                }
            }
        } catch(error) {
            console.log(error)
            ctx.body = {
                success: false,
                data: {},
                info: {msg: "服务器请求异常"},
                code: 500
            }
        }
    },
    addRole: async(ctx,next) => {
        try {
            const {roleName,roleLevel} = ctx.request.body
                const addRoleResult =  await createRole(roleName,roleLevel)
                if(addRoleResult) {
                    ctx.body = {
                        success: true,
                        data: 'addRoleResult:addRoleResult',
                        info: {msg: "添加角色信息成功"},
                        code: 200
                    }
                } else {
                    ctx.body = {
                        success: false,
                        data: 'addRoleResult:addRoleResult',
                        info: {msg: "添加角色信息失败"},
                        code: 200
                    }
                }
        } catch(error) {
            console.log(error)
            ctx.body = {
                success: false,
                data: {},
                info: {msg: "服务器请求异常"},
                code: 500
            }
        }
    },
    deleteRole: async(ctx,next) => {
        try {
            const id = ctx.params.id
            if(id==1){
                ctx.body = {
                    success: false,
                    data: {},
                    info: {msg: "管理员不可删除"},
                    code: 200
                }
            }else{
                const deleteRoleResult = await deleteRoleById(id)
                if(deleteRoleResult) {
                    ctx.body = {
                        success: true,
                        data: {deleteRoleResult:deleteRoleResult},
                        info: {msg: "删除角色信息成功"},
                        code: 200
                    }
                } else {
                    ctx.body = {
                        success: false,
                        data: {},
                        info: {msg: "删除角色信息失败"},
                        code: 200
                    }
                }
            }        
        } catch(error) {
            console.log(error)
            ctx.body = {
                success: false,
                data: {},
                info: {msg: "服务器请求异常"},
                code: 500
            }
        }
    },
    updateRole: async(ctx,next) => {
        try {
            const {id,roleName,roleLevel} = ctx.request.body
            if(id==1){
                ctx.body = {
                    success: false,
                    data: {},
                    info: {msg: "该用户为管理员不可修改"},
                    code: 200
                }
            }else{
                const updateRoleResult =  await updateRole(id,roleName,roleLevel)
                if(updateRoleResult) {
                    ctx.body = {
                        success: true,
                        data: {updateRoleResult:updateRoleResult},
                        info: {msg: "更新角色信息成功"},
                        code: 200
                    }
                } else {
                    ctx.body = {
                        success: false,
                        data: {},
                        info: {msg: "更新角色信息失败"},
                        code: 200
                    }
                }
                
            }
        } catch(error) {
            console.log(error)
            ctx.body = {
                success: false,
                data: {},
                info: {msg: "服务器请求异常"},
                code: 500
            }
        }
    }
}